﻿using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdvanceGridSearchCrm2013
{
    class QueryUtility
    {

        public DataTable ExecuteFetchXml(string fetchXmlFragment, IOrganizationService service)
        {//execute
            FetchExpression fetch = new FetchExpression(fetchXmlFragment)
            ; EntityCollection fetchresults = service.RetrieveMultiple(fetch);
            DataTable resultsTable = new DataTable("results");
            if (fetchresults.Entities.Count > 0)
            {
                for (int i = 0; i < fetchresults.Entities.Count; i++)
                {
                    var entity = fetchresults.Entities[i]; DataRow row = resultsTable.NewRow();
                    foreach (var attribute in entity.Attributes)
                    {
                        if (!resultsTable.Columns.Contains(attribute.Key))
                        { resultsTable.Columns.Add(attribute.Key); } row[attribute.Key] =
                            getAttributeValue(attribute.Value).ToString();
                    }
                    foreach (var fv in entity.FormattedValues)
                    {
                        if (!resultsTable.Columns.Contains(fv.Key + "name"))
                        {
                            resultsTable.Columns.Add(fv.Key + "name");
                        }
                        row[fv.Key + "name"] = fv.Value;
                    }
                    resultsTable.Rows.Add(row);
                }
            } 
            return resultsTable;
        }
        public DataTable GetDataTableFromEntityColc(EntityCollection clc)
        {
            DataTable resultsTable = new DataTable("results");
            if (clc.Entities.Count > 0)
            {
                for (int i = 0; i < clc.Entities.Count; i++)
                {
                    var entity = clc.Entities[i]; 
                    DataRow row = resultsTable.NewRow();
                    foreach (var attribute in entity.Attributes)
                    {
                        if (!resultsTable.Columns.Contains(attribute.Key))
                        { resultsTable.Columns.Add(attribute.Key); } row[attribute.Key] =
                            getAttributeValue(attribute.Value).ToString();
                    }
                    foreach (var fv in entity.FormattedValues)
                    {
                        if (!resultsTable.Columns.Contains(fv.Key + "name"))
                        {
                            resultsTable.Columns.Add(fv.Key + "name");
                        }
                        row[fv.Key + "name"] = fv.Value;
                    }
                    resultsTable.Rows.Add(row);
                }
            }
            return resultsTable;
        }
        private object getAttributeValue(object entityValue)
        {
            object output = "";
            switch (entityValue.ToString())
            {
                case "Microsoft.Xrm.Sdk.EntityReference":
                    output = ((EntityReference)entityValue).Name;
                    break;
                case "Microsoft.Xrm.Sdk.OptionSetValue":
                    output = ((OptionSetValue)entityValue).Value.ToString();
                    break;
                case "Microsoft.Xrm.Sdk.Money":
                    output = ((Money)entityValue).Value.ToString();
                    break;
                case "Microsoft.Xrm.Sdk.AliasedValue":
                    output = getAttributeValue(((Microsoft.Xrm.Sdk.AliasedValue)entityValue).Value);
                    break;
                default:
                    output = entityValue.ToString();
                    break;
            }
            return output;
        }
    }
}
